package cn.tedu;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;

import java.io.IOException;

public class MusicDriver {
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.rootdir", "hdfs://hadoop1:9000/hbase");
        conf.set("hbase.zookeeper.quorum", "hadoop1");
        Job job = Job.getInstance(conf,"top-music");
        //MapReduce程序作业基本配置
        job.setJarByClass(MusicDriver.class);
        job.setNumReduceTasks(1);
        //为music表设置过滤条件，只保留歌名name
        Scan scan = new Scan();
        scan.addColumn(Bytes.toBytes("info"), Bytes.toBytes("name"));
        //使用HBase提供的工具类来设置job，这里导包注意import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
        TableMapReduceUtil.initTableMapperJob("music", scan, MusicMapper.class, Text.class, IntWritable.class, job);
        TableMapReduceUtil.initTableReducerJob("namelist", MusicReducer.class, job);
        job.waitForCompletion(true);
        System.out.println("执行成功，统计结果存于namelist中");
    }
}
